<?php

class GL_Purchase extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->layout->setLayout("home/layout");
        if ($this->permission() == 3) redirect('../');
    }

    public function cart()
    {
        $session = $this->session->userdata('logged_in');
        $uid = $session['id'];

        $products = $this->purchasing->get_all_purchasing($uid);

        foreach ($products as $product) {
            $promo = $this->product->get_promotion($product->id);
            if ($promo&&$promo->start<=time()&&$promo->end>time())
                $product->price = round($product->price - $promo->discount/100*$product->price,1);
        }


        $data['products'] = $products;

        for ($i = 0; $i < count($data['products']); $i++) {
            $data['images'][$i] = $this->product->get_images($data['products'][$i]->id)[0];
        }

        $this->layout->view('home/cart',$data);
    }

    public function remove_item(){
        $session = $this->session->userdata('logged_in');
        $uid = $session['id'];

        $pid = $this->input->post('pid');
        echo $this->purchasing->remove($uid,$pid);
    }

    public function checkout(){
        $pids = $this->input->post('pids');
        if (!isset($pids)) redirect('../');

        $data['pids'] = $pids;
        $pids = explode(',',$pids);
        $products = array();

        foreach ($pids as $pid) {
            $pid = intval($pid);
            array_push($products,$this->product->get($pid)[0]);
        }

        foreach ($products as $product) {
            $promo = $this->product->get_promotion($product->id);
            if ($promo&&$promo->start<=time()&&$promo->end>time())
                $product->price = round($product->price - $promo->discount/100*$product->price,1);
        }

        $data['products'] = $products;
        $total = 0;

        for ($i = 0; $i < count($data['products']); $i++) {
            $data['images'][$i] = $this->product->get_images($data['products'][$i]->id)[0];
            $total += $data['products'][$i]->price;
        }

        $data['total'] = $total;

        $this->layout->view('home/checkout',$data);
    }

    public function checkout_step2(){
        $total = $this->input->post('total');
        $pids = $this->input->post('pids');
        if (!isset($pids)) redirect('../');

        $sess = array(
            'pids' => $pids,
            'total' => $total
        );

        $this->session->set_userdata('pids', $sess);

        echo 1;


    }

    public function checkout_paypal(){

        $session_data = $this->session->userdata('pids');

        $cer = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 100);
        $amount = $session_data['total'];

        $this->load->library('merchant');
        $this->merchant->load('paypal_express');

        $settings = array(
            'username' => 'hungbya-facilitator_api1.gmail.com',
            'password' => '9L7ASG6WBRS3BSYN',
            'signature' => 'AFcWxV21C7fd0v3bYYYRCpSSRl31AtA5sG3dYS.GjXSIOmU8yoTlqkpy',
            'test_mode' => true);

        $this->merchant->initialize($settings);

        $sess = array(
            'cer' => $cer,
            'amount' =>$amount,
        );


        $this->session->set_userdata('payment', $sess);

        $params = array(
            'amount' => $amount,
            'currency' => 'USD',
            'return_url' => 'http://localhost/purchase/checkout_paypal_step2?cer='.$cer,
            'cancel_url' => 'http://localhost/purchase/checkout_paypal_cancel');

        $response = $this->merchant->purchase($params);

        $message = $response->message();
        echo('Error processing payment: ' . $message);
    }

    public function checkout_paypal_step2(){
        $cer = $this->input->get('cer');

        $session_data = $this->session->userdata('payment');

        $_cer = $session_data['cer'];
        $amount = $session_data['amount'];

        if ($_cer==$cer) {
            $this->load->library('merchant');
            $this->merchant->load('paypal_express');

            $settings = array(
                'username' => 'hungbya-facilitator_api1.gmail.com',
                'password' => '9L7ASG6WBRS3BSYN',
                'signature' => 'AFcWxV21C7fd0v3bYYYRCpSSRl31AtA5sG3dYS.GjXSIOmU8yoTlqkpy',
                'test_mode' => true);

            $this->merchant->initialize($settings);

            $params = array(
                'amount' => $amount,
                'currency' => 'USD',
                'return_url' => 'http://localhost/purchase/checkout_paypal_step2?cer='.$cer,
                'cancel_url' => 'http://localhost/purchase/checkout_paypal_cancel');

            $response = $this->merchant->purchase_return($params);

            if ($response->success())
            {
                $session_data = $this->session->userdata('pids');

                $pids = $session_data['pids'];

                $session = $this->session->userdata('logged_in');
                $uid = $session['id'];
                $pids = explode(',',$pids);

                foreach ($pids as $pid) {
                    $pid = intval($pid);
                    $this->purchasing->set_purchased($uid,$pid);
                }
                $this->session->unset_userdata('payment');
                $this->session->unset_userdata('pids');
                redirect('http://localhost/purchased');
            }
            else
            {
                $message = $response->message();
                echo('Error processing payment: ' . $message);
                exit;
            }
        }
        else {
            redirect('http://localhost/purchase/checkout_paypal_cancel');
        }
    }

    public function checkout_paypal_cancel(){
        $this->session->unset_userdata('payment');
        $this->session->unset_userdata('pids');
        redirect('http://localhost/cart');
    }
}